package dtd.PHS.VND_ExchangeRate.data;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import dtd.PHS.VND_ExchangeRate.utils.Logger;

public class SJCGoldParser {
	private static final String UPDATE_TIME = "<div class=\"w350 m5l float_left red_text bg_white\">";
	private static final String SJC_URL = "http://www.sjc.com.vn/wap/textContent.php";
	private static final String SJC_HCM_1L = "<td align=\"center\" class=\"br bb bg_bl white_text\"><span style=\"font-size:larger\">";
	private Date updateTime;

	private SJCItem sjcPrice;
	
	public SJCGoldParser() {		
		updateTime = null;
	}
	
	public void parse() {
		try {
			URL url = new URL(SJC_URL);
			BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(url.openStream()));
			readUpdateTime(bufferedReader);
			readHCM(bufferedReader);
			sjcPrice.setUpdateTime(updateTime);
			bufferedReader.close();
		} catch (MalformedURLException e) {
			Logger.logError(e);
		} catch (IOException e) {			
			Logger.logError(e);
		} catch (Exception e) {
			Logger.logError(e);
		}
	}

	private void readHCM(BufferedReader bufferedReader) throws IOException {
		String inputLine = null;
		while ( (inputLine = bufferedReader.readLine() ) != null) {
			if ( inputLine.contains(SJC_HCM_1L) ) {
				extractSJCHCM(bufferedReader,inputLine);
				break;
			}
		}
	}

	private void readUpdateTime(BufferedReader bufferedReader)
			throws IOException {
		String inputLine;
		while ( (inputLine = bufferedReader.readLine() ) != null) {
			if ( inputLine.contains(UPDATE_TIME) ) { 
				extractTime(inputLine);
				break;
			}
		}
	}

	static final String PRICE_BEG = "<td align=\"center\" class=\"br bb bg_bl white_text\"><span style=\"font-size:larger\">";
	static final String PRICE_END = "</span>";
	private void extractSJCHCM(BufferedReader bufferedReader, String inputLine) throws IOException {
		int bpos = inputLine.indexOf(PRICE_BEG) + PRICE_BEG.length();
		int epos = inputLine.indexOf(PRICE_END,bpos);
		String buyingStr = inputLine.substring(bpos,epos).trim().replace(",", "");
		inputLine = bufferedReader.readLine();
		bpos = inputLine.indexOf(PRICE_BEG) + PRICE_BEG.length();
		epos = inputLine.indexOf(PRICE_END);
		String sellingStr = inputLine.substring(bpos, epos).trim().replace(",", "");;
		
		try {
			int buying = Integer.parseInt(buyingStr);
			int selling = Integer.parseInt(sellingStr);
			this.sjcPrice = new SJCItem(buying,selling);
		} catch (Exception e) {
			Logger.logError(e);
		}
	}
	static final String UPDATE_STR_BEG = "<div class=\"w350 m5l float_left red_text bg_white\">";
	static final String UPDATE_STR_END= "</div>";
	private void extractTime(String inputLine) {
		int bpos = inputLine.indexOf(UPDATE_STR_BEG) + UPDATE_STR_BEG.length();
		int epos = inputLine.indexOf(UPDATE_STR_END,bpos);
		String time = inputLine.substring(bpos, epos).trim().replace("</font>","");
		SimpleDateFormat df = new SimpleDateFormat("hh:mm:ss aa dd/MM/yyyy");
		try {
			updateTime = df.parse(time);
		} catch (ParseException e) {
			Logger.logError(e);
		}
	}
	public Date getUdapteTime() {
		return updateTime;
	}
	
	public SJCItem getPrice() {
		return sjcPrice;
	}



}
